package com.ayuku.retail.ui.pembelian;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.ListFormMaster;
import com.ayuku.commons.Util;
import com.ayuku.retail.business.purchase.PurchaseImpl;
import com.ayuku.retail.persistence.model.Purchase;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ListPurchase extends ListFormMaster {
	private FrmPurchase frm;

	public ListPurchase(Shell parent, int style) {
		super(parent, style);
	}
  
	@Override
	public void showEditForm() {
		TableItem[] selection = tableData.getSelection();

		if ((selection != null) && (selection.length > 0)) {
			TableItem itm = (TableItem) selection[0];
			frm = new FrmPurchase(getDialogShell(), SWT.NULL,
					AYConst.MODE_VIEW);
			frm.setFormKey(itm.getText(0));
			frm.setFuncId(getFuncId());
			frm.open();
		} else {
			Util.showMsgBox(AYConst.FAIL_VIEW, AYConst.MSG_WARN,
					getDialogShell());
		}
	}

	@Override
	public Integer doDelete(List<String> lst) throws Exception {
		return PurchaseImpl.delete(lst);
	}

	@Override
	public void loadSearchData() throws Exception {
		populateTable("%" + getFSearch().getText() + "%");
	}

	@Override
	public void loadAllData() throws Exception {
		populateTable("%%");
	}

	@Override
	public void showAddForm() {
		frm = new FrmPurchase(getDialogShell(), SWT.NULL, AYConst.MODE_ADD);
		frm.setFuncId(getFuncId());
		frm.open();
	}

	@Override
	public void populateTable(String arg) throws Exception {
		tableData.removeAll();
		List<Purchase> lst = PurchaseImpl.getList(arg);
		tableData.setItemCount(lst.size());
		data = new Object[lst.size()][];

		for (int i = 0; i < lst.size(); i++) {
			Purchase pj = lst.get(i);
			data[i] = new String[] { Util.nullToString(pj.getTrxCode()),
					Util.dateToString(pj.getTrxDate()),
					Util.formatToNumber(pj.getGrandTotal()),
					Util.nullToString(pj.getIsLunas()),
					Util.nullToString(pj.getBayar()),
					Util.nullToString(pj.getSupplier()) };
		}

	}

	@Override
	public void additionalProcess() {
		getDialogShell().setText("Data Pembelian");
		getDialogShell().setSize(780, 540);
		Util.setCenter(getDialogShell());
	}

	@Override
	public void printPreview() {
	}

	@Override
	public String[] getColumCaption() {
		final String[] titles = {"No Transaksi/Nota","Tanggal","Total", "Lunas","Bayar", "Supplier" };
		return titles;
	}

	@Override
	public int[] getColumWidth() {
		int[] i = { 120, 120, 120, 180 , 120, 180};
		return i;
	}

	@Override
	protected void fillTable(Object[] obj, TableItem item) {
		item.setText(0, Util.nullToString(obj[0]));
		item.setText(1, Util.nullToString(obj[1]));
		item.setText(2, Util.nullToString(obj[2]));
		item.setText(3, Util.nullToString(obj[3]));
		item.setText(4, Util.nullToString(obj[4]));
		item.setText(5, Util.nullToString(obj[5]));		
	}

}
